package com.ruoyi.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.service.IFinRecordVoucherItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import javax.annotation.Resource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.FinRecordVoucher;
import com.ruoyi.project.service.IFinRecordVoucherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 记账凭证Controller
 * 
 * @author abel
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/project/FinRecordVoucher")
public class FinRecordVoucherController extends BaseController
{
    @Resource
    private IFinRecordVoucherService finRecordVoucherService;
    @Resource
    private IFinRecordVoucherItemService finRecordVoucherItemService;
    /**
     * 查询记账凭证列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:list')")
    @GetMapping("/list")
    public TableDataInfo list(FinRecordVoucher finRecordVoucher)
    {
        startPage();
        finRecordVoucher.setPackageId(ServletUtils.getPackageId());
        List<FinRecordVoucher> list = finRecordVoucherService.selectFinRecordVoucherList(finRecordVoucher);
        list.forEach(recordVoucher ->{
            recordVoucher.setItemList(finRecordVoucherItemService.listByRecordId(recordVoucher.getId()));
        });
        return getDataTable(list);
    }

    /**
     * 导出记账凭证列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:export')")
    @Log(title = "记账凭证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinRecordVoucher finRecordVoucher)
    {
        List<FinRecordVoucher> list = finRecordVoucherService.selectFinRecordVoucherList(finRecordVoucher);
        ExcelUtil<FinRecordVoucher> util = new ExcelUtil<FinRecordVoucher>(FinRecordVoucher.class);
        util.exportExcel(response, list, "记账凭证数据");
    }

    /**
     * 获取记账凭证详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        FinRecordVoucher finRecordVoucher = finRecordVoucherService.getFinRecordVoucherById(id);
        finRecordVoucher.setItemList(finRecordVoucherItemService.listByRecordId(finRecordVoucher.getId()));
        return success(finRecordVoucher);
    }

    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:query')")
    @PutMapping(value = "/nextInfo")
    public AjaxResult getNextInfo(@RequestBody FinRecordVoucher finRecordVoucher)
    {
        FinRecordVoucher nextVoucher = finRecordVoucherService.findNextByInfo(finRecordVoucher.getPackageId(),finRecordVoucher.getRecordTag(),
                finRecordVoucher.getYearValue(),finRecordVoucher.getMonthValue(),finRecordVoucher.getRecordNo());
        if(nextVoucher!=null){
            nextVoucher.setItemList(finRecordVoucherItemService.listByRecordId(nextVoucher.getId()));
        }
        return success(nextVoucher);
    }

    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:query')")
    @PutMapping(value = "/preInfo")
    public AjaxResult getPreInfo(@RequestBody FinRecordVoucher finRecordVoucher)
    {
        FinRecordVoucher preVoucher = finRecordVoucherService.findPreByInfo(finRecordVoucher.getPackageId(),finRecordVoucher.getRecordTag(),
                finRecordVoucher.getYearValue(),finRecordVoucher.getMonthValue(),finRecordVoucher.getRecordNo());
        if(preVoucher!=null){
            preVoucher.setItemList(finRecordVoucherItemService.listByRecordId(preVoucher.getId()));
        }
        return success(preVoucher);
    }

    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:query')")
    @GetMapping(value = "/getMaxRecordNo/{recordTag}/{currentYear}/{currentMonth}")
    public AjaxResult getMaxRecordNo(@PathVariable("currentYear") Integer currentYear,@PathVariable("currentMonth") Integer currentMonth
                                     ,@PathVariable("recordTag")String recordTag)
    {
        return success(finRecordVoucherService.getMaxRecordNo(ServletUtils.getPackageId(),recordTag,currentYear,currentMonth));
    }

    /**
     * 新增记账凭证
     */
    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:add')")
    @Log(title = "记账凭证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinRecordVoucher finRecordVoucher)
    {
        finRecordVoucher.setPackageId(ServletUtils.getPackageId());
        return AjaxResult.success(finRecordVoucherService.insertFinRecordVoucher(finRecordVoucher));
    }

    /**
     * 修改记账凭证
     */
    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:edit')")
    @Log(title = "记账凭证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinRecordVoucher finRecordVoucher)
    {
        return toAjax(finRecordVoucherService.updateFinRecordVoucher(finRecordVoucher));
    }

    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:edit')")
    @Log(title = "记账凭证审核", businessType = BusinessType.UPDATE)
    @PutMapping("/check/{id}/{result}")
    public void check(@PathVariable boolean result,@PathVariable Long id)
    {
        finRecordVoucherService.check(id,result);
    }


    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:edit')")
    @Log(title = "记账凭证记账，true记账", businessType = BusinessType.UPDATE)
    @PutMapping("/recordToBook/{id}")
    public void recordToBook(@PathVariable Long id)
    {
        finRecordVoucherService.recordToBook(id,true);
    }

    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:edit')")
    @Log(title = "反记账", businessType = BusinessType.UPDATE)
    @PutMapping("/recordCancelToBook/{id}")
    public void recordCancelToBook(@PathVariable Long id)
    {
        finRecordVoucherService.recordToBook(id,false);
    }

    /**
     * 删除记账凭证
     */
    @PreAuthorize("@ss.hasPermi('project:FinRecordVoucher:remove')")
    @Log(title = "记账凭证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(finRecordVoucherService.deleteFinRecordVoucherByIds(ids));
    }
}
